import type { RouteRecordNormalized } from 'vue-router';
import { AppRouteRecordRaw } from './types';
import SYSTEM from './modules/system';
import USER from './modules/user';
import STATION from './modules/station';
import DASHBOARD from './modules/dashboard';
import VISUALIZATION from './modules/visualization';
import LIST from './modules/list';
import FORM from './modules/form';
import PROFILE from './modules/profile';
import RESULT from './modules/result';
import EXCEPTION from './modules/exception';
import CHARGING from './modules/charging';
import PARTNER from './modules/partner';
import ORDER from './modules/order';
import PROFIT from './modules/profit';

const modules = import.meta.glob('./modules/*.ts', { eager: true });
const externalModules = import.meta.glob('./externalModules/*.ts', {
  eager: true,
});

function formatModules(_modules: any, result: RouteRecordNormalized[]) {
  Object.keys(_modules).forEach((key) => {
    const defaultModule = _modules[key].default;
    if (!defaultModule) return;
    const moduleList = Array.isArray(defaultModule)
      ? [...defaultModule]
      : [defaultModule];
    result.push(...moduleList);
  });
  return result;
}

// 使用手动导入的方式定义路由
export const appRoutes: AppRouteRecordRaw[] = [
  DASHBOARD,
  VISUALIZATION,
  LIST,
  FORM,
  PROFILE,
  RESULT,
  EXCEPTION,
  USER,
  SYSTEM,
  STATION,
  CHARGING,
  PARTNER,
  ORDER,
  PROFIT,
];

export const appExternalRoutes: RouteRecordNormalized[] = formatModules(
  externalModules,
  []
);
